package com.example.demo.mapper;

import com.example.demo.bean.Car;
import com.example.demo.bean.ChildCar;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface CarMapper {
    @Select("select poi_id from car where car_id = #{car_id}")
    int getCarPosition(@Param("car_id") int car_id);

    @Update("update car set poi_id =#{poi_id} where car_id =#{car_id}")
    void changeCarPosition(@Param("car_id") int car_id,@Param("poi_id") int destination);

    @Update("update car set run_message_id = #{run_message_id} where car_id = #{car_id}")
    void changeCar(@Param("car_id") int car_id,@Param("run_message_id") int message_id);

    @Update("update car set run_message_id = 0 where run_message_id = -1")
    void acceptCars();

    @Select("select COUNT(car_id) from car where run_message_id = #{run_message_id}")
    int getCar_id_num(@Param("run_message_id") int message_id);

    @Select("select status_id from car where car_id = #{car_id}")
    Map<String,Object> getCarStatus(@Param("car_id") int car_id);

    @Update("update car set status_id = #{status_id} where car_id = #{car_id}")
    void changeStatus(@Param("status_id") int status_id,@Param("car_id") int car_id);

    @Update("update car set industry_id = #{industry_id} where car_id = #{car_id}")
    void addIndustry(@Param("industry_id") int industry_id,@Param("car_id") int car_id);

    @Update("update car set industry_id = null where car_id = #{car_id}")
    void delIndustry(@Param("car_id") int car_id);

    @Update("update car set run_message_id = 0 where car_id = #{car_id}")
    void releaseCar(@Param("car_id") int car_id);

    @Select("select car_id,poi_id from car where run_message_id = 0 and status_id = 5")
    List<Car> getCars();

    @Select("select run_message_id from car where car_id = #{car_id}")
    int selectRunMessage(@Param("car_id") int car_id);

    @Select("SELECT COUNT(*) FROM car")
    int selectCar_num();

    @Select("select * from car where car_id = #{car_id} for update")
    Car lock(@Param("car_id") int car_id);

    @Select("select car.car_id,car.poi_id,vehicle.load_m,distance.Duration,distance.Distance " +
            "from car join vehicle on car.vehicle_type = vehicle.vehicle_id " +
            "join distance on distance.end_poi_id = car.poi_id " +
            "where car.status_id = 5 " +
            "and car.run_message_id = 0 " +
            "and distance.start_poi_id = #{poi_id} " +
            "order by (1+abs(vehicle.load_m-#{need_remain})/vehicle.load_m * distance.Distance) " +
            "LIMIT 75")
    List<ChildCar> search_part_cars(@Param("poi_id") int previous_id, @Param("need_remain") double need_remain);
}

